import com.alibaba.fastjson.JSONObject;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.Base64;

public class IMConnectMain {
    public static void main(String[] args) throws URISyntaxException {
        WebSocketClient webSocketClient = new WebSocketClient(new URI("ws://192.168.2.139:8803/chatsocket?request=9J0IuyZyjVjCiVW7Eo9y%2FYLlSjGf7n5C4zsPvdzObpIT%2FAW78XndpM%2BmVcGSJrwWfqK%2FC%2FgBH%2BUGIjkez9fi0zdg6UbMPA%2B6zHj02vgBnQnj4W2ONZZxerh%2BKj3Sac3cjEqRhJMpWFN%2FgFFpEInQAQBOnnS%2FKeZjeRpj66JdN%2B5HVS4XUB74J%2F7cHHYQjPR6m5nhrBnocw3BlegoJGYEzX%2FvTBUcd3DHbp14MlZHfFUF6azPxt5UCv20w5uw76LPVYSiCLhuStUa6eJmR0hAxbQFyCOfqB95Ciok3H2z%2FUHl9bojr%2BotiEVP6fGkQ4wdJYgIQywInVDfWvRshxohRV1YK509vM89skctlN6ic3dgArXPkzqpu9wew6YwdIwreEbiLajzfApbMLxHkObXkQ%3D%3D"))
//        WebSocketClient webSocketClient = new WebSocketClient(new URI("ws://82.157.123.54:9010/ajaxchattest"))
        {
            private boolean openFlag = false;
            @Override
            public void onOpen(ServerHandshake serverHandshake) {
                System.out.println("IM链接成功");
                openFlag = true;
                while (openFlag){
                    send("111111");
                    try {
                        Thread.sleep(1000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onMessage(String s) {
                System.out.println("Im收到消息");
            }

            @Override
            public void onClose(int i, String s, boolean b) {
                openFlag = false;
                System.out.println("IM断开链接");
            }

            @Override
            public void onError(Exception e) {
                System.out.println("IM发生错误"+e.getMessage());
            }
        };
        webSocketClient.connect();
    }

    /**
     * 发送消息
     * @param client 客户端
     * @param actionId 事件
     * @param data  数据
     * @param fanout 是否广播消息
     */
    private void send(WebSocketClient client, Integer actionId, JSONObject data,boolean fanout){
        try {
            String encode = URLEncoder.encode(data.toJSONString(), "utf-8");
            byte[] encode1 = Base64.getEncoder().encode(encode.getBytes());
            int zipEncode = fanout ? 11 : 1;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }




}
